package com.ocm.tourism.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ocm.tourism.core.dao.BusinessDao;
import com.ocm.tourism.core.dao.CommentDao;
import com.ocm.tourism.core.domain.Business;
import com.ocm.tourism.core.domain.Comment;

@Service
public class CommentServiceImpl implements CommentService {

   @Autowired
   private CommentDao commentDao;

   @Autowired
   private BusinessDao businessDao;

   @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
   public void save(Comment comment) {
      this.commentDao.save(comment);
   }

   @Override
   public List<Comment> findComments(String attractionType, int firstResult, int sizeNo) {
      List<Comment> comments = this.commentDao.getAttractionComments(attractionType, firstResult, sizeNo);
      return comments;
   }

   @Override
   public long count(String attractionType) {
      long count = this.commentDao.count(attractionType);
      return count;
   }

   @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
   public void delete(long commentId) {
      this.commentDao.remove(commentId);
   }

   @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
   public void saveBusinessComment(long businessId, Comment comment) {
      Business business = this.businessDao.find(businessId);
      business.setLatestComment(comment);
      this.businessDao.save(business);
   }

   @Override
   public List<Comment> findCommentsByCatalogue(long catelogueId, int firstResult, int sizeNo) {
      List<Comment> comments = this.commentDao.getCommentsByCatalogue(catelogueId, firstResult, sizeNo);
      return comments;
   }

   @Override
   public long countByCatalogue(long catalogueId) {
      long count = this.commentDao.countByCatalogue(catalogueId);
      return count;
   }

   @Override
   public long countByBusiness(long businessId) {
      long count = this.commentDao.countByBusiness(businessId);
      return count;
   }

   @Override
   public List<Comment> findCommentsByTour(long tourId, int firstResult, int sizeNo) {
      List<Comment> comments = this.commentDao.getCommentsByTour(tourId, firstResult, sizeNo);
      return comments;
   }

   @Override
   public List<Comment> findCommentsByTourSale(long tourSaleId, int firstResult, int sizeNo) {
      List<Comment> comments = this.commentDao.getCommentsByTourSale(tourSaleId, firstResult, sizeNo);
      return comments;
   }

   @Override
   public long countByTourSale(long tourSaleId) {
      long count = this.commentDao.countByTourSale(tourSaleId);
      return count;
   }

   @Override
   public long countByTour(long tourId) {
      long count = this.commentDao.countByTour(tourId);
      return count;
   }
}
